package com.dataart.productshop.controller;

import com.dataart.productshop.Constants;
import com.dataart.productshop.model.boiko.informationsystem.Kathedra;
import com.dataart.productshop.model.boiko.informationsystem.Lecturer;
import com.dataart.productshop.model.boiko.informationsystem.Student;
import com.dataart.productshop.model.boiko.informationsystem.Subject;
import com.dataart.productshop.service.KathedraManager;
import com.dataart.productshop.service.LecturerManager;
import com.dataart.productshop.service.SubjectManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class LecturerController {


    private LecturerManager lecturerManager;

    private SubjectManager subjectManager;

    private KathedraManager kathedraManager;



    @RequestMapping(value = "/lecturers", method = RequestMethod.GET)
    public ModelAndView getLecturersList(){
        return generateStandartPage();
    }

    @RequestMapping(value = "/lecturers/addnew", method = RequestMethod.GET)
    public ModelAndView askForCreateNewLecturer(){
        Map<String, Object> pagePack = new HashMap<String, Object>();
        pagePack.put(Constants.SUBJECT_LIST, subjectManager.getAll());
        pagePack.put(Constants.KATHEDRA_LIST, kathedraManager.getAll());
        return new ModelAndView(Constants.JspNames.LECTURER_ADD, pagePack);
    }

    @RequestMapping(value = {"/lecturers/addnew",
            "/edit" + Constants.LECTURER_TYPE + "/{itemId}"}
            , method = RequestMethod.POST)
    public ModelAndView createNewLecturer(
            @RequestParam(value = "itemId", required = false)String lecturerId,
            @RequestParam(value = "kathedra", required = false)String kathedraId,
            @RequestParam(value = "subject", required = false) String subjectId,
            @RequestParam(value = "lecturerName", required = false)String lecturerName){

        if(kathedraId != null && subjectId != null
                && lecturerName != null && !lecturerName.isEmpty()
                && noLecturerWithSuchName(lecturerName)){
            try {
                Long subjId = Long.parseLong(subjectId);
                Long kathId = Long.parseLong(kathedraId);

                Subject subj = new Subject();
                subj.setId(subjId);

                Kathedra kathedra = new Kathedra();
                kathedra.setId(kathId);

                Lecturer newLecturer;
                if(lecturerId != null && !lecturerId.isEmpty()){
                    newLecturer = lecturerManager.getById(Long.parseLong(lecturerId));
                } else {
                    newLecturer = new Lecturer();
                }
                newLecturer.setName(lecturerName);
                newLecturer.setKathedra(kathedra);
                newLecturer.setSubject(subj);

                lecturerManager.save(newLecturer);

            }catch(NumberFormatException ex){

            }
        }
        return generateStandartPage();
    }

    @RequestMapping(value = "/remove" + Constants.LECTURER_TYPE + "/{itemId}"
            , method = RequestMethod.GET)
    public String removeStudent(@PathVariable("itemId") String itemId){

        try{
            lecturerManager.delete(Long.parseLong(itemId));
        }catch (NumberFormatException e){

        }catch(Exception dd){
            System.err.println("some exception were occured in AssKathedra controller"
                    + dd.getStackTrace());
        }
        return "redirect:/lecturers";

    }

    @RequestMapping(value = "/edit" + Constants.LECTURER_TYPE + "/{itemId}"
            , method = RequestMethod.GET)
    public ModelAndView editStudent(@PathVariable("itemId") String itemId){

        try{
            Lecturer lecturer = lecturerManager.getById(Long.parseLong(itemId));
            Map<String, Object> map = new HashMap<String, Object>();
            map.put(Constants.SUBJECT_LIST, subjectManager.getAll());
            map.put(Constants.KATHEDRA_LIST, kathedraManager.getAll());
            map.put(Constants.LECTURER_TYPE, lecturerManager.getById(Long.parseLong(itemId)));
            return new ModelAndView("addLecturer", map);

        }catch (NumberFormatException e){

        }catch(Exception dd){
            System.err.println("some exception were occured in AssKathedra controller"
                    + dd.getStackTrace());
        }
        return generateStandartPage();
    }

    private boolean noLecturerWithSuchName(String name){
        return lecturerManager.getLecturerByName(name).isEmpty();
    }

    private ModelAndView generateStandartPage(){
        Map<String, Object> pagePack = new HashMap<String, Object>();
        List<Lecturer> allLecturers = lecturerManager.getAll();
        pagePack.put("items", allLecturers);
        return new ModelAndView(Constants.JspNames.LECTURERS, pagePack);
    }

    @Autowired
    public void setLecturerManager(LecturerManager lecturerManager) {
        this.lecturerManager = lecturerManager;
    }

    @Autowired
    public void setSubjectManager(SubjectManager subjectManager) {
        this.subjectManager = subjectManager;
    }

    @Autowired
    public void setKathedraManager(KathedraManager kathedraManager) {
        this.kathedraManager = kathedraManager;
    }
}
